package org.orbis.spikezolt;

import giannign1.gSprites.CompleteTouchEvent;
import giannign1.gSprites.GAnimatedSprite;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;

public class GameHUD {
	final BaseGameActivity bga;
	private int camWidth, camHeight;
	private final Player player;

	private BoundCamera cam;

	// Buttons
	private BitmapTextureAtlas buttonBitmapTextureAtlas;
	private TiledTextureRegion buttonLeftRegion;
	private TiledTextureRegion buttonRightRegion;
	private TiledTextureRegion buttonUpRegion;
	public GAnimatedSprite buttonLeft;
	public GAnimatedSprite buttonRight;
	public GAnimatedSprite buttonUp;

	public GameHUD(BaseGameActivity bga, Scene scene, HUD hud, Engine engine,
			final Player player, int camWidth, int camHeight, BoundCamera cam) {
		this.cam = cam;
		this.bga = bga;
		this.player = player;
		this.camWidth = camWidth;
		this.camHeight = camHeight;

		final Display display = bga.getWindowManager().getDefaultDisplay();
		int cameraWidth = display.getWidth();
		int cameraHeight = display.getHeight();

		buttonBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		buttonLeftRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(buttonBitmapTextureAtlas, bga,
						"gfx/Buttons/butLeft_texture.png", 0, 0, 2, 1);

		buttonRightRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(buttonBitmapTextureAtlas, bga,
						"gfx/Buttons/butRight_texture.png", 100, 0, 2, 1);

		buttonUpRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(buttonBitmapTextureAtlas, bga,
						"gfx/Buttons/butUp_texture.png", 200, 0, 2, 1);

		engine.getTextureManager().loadTexture(buttonBitmapTextureAtlas);

		int padding = 5;
		int farXValue = 320 - 50 - padding;
		int yValue = 240 - buttonLeftRegion.getHeight() - padding - 5;

		buttonLeft = new GAnimatedSprite(padding, yValue, buttonLeftRegion) {

			@Override
			public boolean onAreaTouched(CompleteTouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {

				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_DOWN) {
					// this.setCurrentTileIndex(1);
					if (player != null) {
						player.leftPush(true);
						player.leftReleased = false;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_MOVE) {
					/*
					 * this.setCurrentTileIndex(1); player.leftPush(true);
					 * player.leftReleased = false;
					 */
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_UP) {
					// this.setCurrentTileIndex(0);
					if (player != null) {
						player.leftPush(false);
						player.leftReleased = true;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_OUTSIDE) {
					// this.setCurrentTileIndex(0);
					if (player != null) {
						// player.leftPush(false);
						player.leftReleased = true;
					}
				}

				return false;
			}

		};

		buttonLeft.setCurrentTileIndex(0);
		buttonLeft.setAlpha(0.5f);
		// buttonLeft.setFromOutsideActionMoveEnabled(false);
		// buttonLeft.setFromOutsideActionUpEnabled(false);
		// buttonLeft.setFromOutsideActionOutsideEnabled(false);

		buttonRight = new GAnimatedSprite(padding + 60, yValue,
				buttonRightRegion) {
			@Override
			public boolean onAreaTouched(CompleteTouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {

				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_DOWN) {
					// this.setCurrentTileIndex(1);
					if (player != null) {
						player.rightPush(true);
						player.rightReleased = false;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_MOVE) {
					/*
					 * this.setCurrentTileIndex(1); player.rightPush(true);
					 * player.rightReleased = false;
					 */
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_UP) {
					// this.setCurrentTileIndex(0);
					if (player != null) {
						player.rightPush(false);
						player.rightReleased = true;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_OUTSIDE) {
					// this.setCurrentTileIndex(0);
					// player.rightPush(false);
					if (player != null) {
						player.rightReleased = true;
					}
				}

				return false;
			}

		};
		buttonRight.setCurrentTileIndex(0);
		buttonRight.setAlpha(0.5f);
		// buttonRight.setFromOutsideActionMoveEnabled(false);
		// buttonRight.setFromOutsideActionUpEnabled(false);
		// buttonRight.setFromOutsideActionOutsideEnabled(false);

		buttonUp = new GAnimatedSprite(farXValue, yValue, buttonUpRegion) {
			@Override
			public boolean onAreaTouched(CompleteTouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {

				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_DOWN) {
					// this.setCurrentTileIndex(1);
					if (player != null) {
						player.upPush(true);
						player.upReleased = false;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_MOVE) {
					// this.setCurrentTileIndex(1);
					// player.upPush(true);
					// player.upReleased = false;
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_UP) {
					// this.setCurrentTileIndex(0);
					if (player != null) {
						player.upPush(false);
						player.upReleased = true;
					}
				}
				if (pSceneTouchEvent.getAction() == CompleteTouchEvent.ACTION_OUTSIDE) {
					// this.setCurrentTileIndex(0);
					if (player != null) {
						player.upPush(false);
						player.upReleased = true;
					}
				}

				return false;
			}

		};
		buttonUp.setCurrentTileIndex(0);
		buttonUp.setAlpha(0.5f);
		// buttonUp.setFromOutsideActionMoveEnabled(false);
		// buttonUp.setFromOutsideActionUpEnabled(false);
		// buttonUp.setFromOutsideActionOutsideEnabled(false);

		hud.setTouchAreaBindingEnabled(true);

		scene.setChildScene(hud);
		hud.attachChild(buttonLeft);
		hud.attachChild(buttonRight);
		hud.attachChild(buttonUp);
		hud.registerTouchArea(buttonLeft);
		hud.registerTouchArea(buttonRight);
		hud.registerTouchArea(buttonUp);

	}

	public boolean IsWithinDistance(float pointX, float pointY, float rectX,
			float rectY, float rectWidth, float rectHeight,
			float distanceThreshold) {
		float x2 = rectX + rectWidth;
		float y2 = rectY + rectHeight;
		float xDiff = (pointX < rectX) ? rectX - pointX : pointX - x2;
		float yDiff = (pointY < rectY) ? rectY - pointY : pointY - y2;
		float distance2;

		xDiff = (xDiff < 0) ? 0 : xDiff;
		yDiff = (yDiff < 0) ? 0 : yDiff;

		distance2 = xDiff * xDiff + yDiff * yDiff;

		return distance2 < (distanceThreshold * distanceThreshold);
	}

	public void myTouch(Scene pScene, TouchEvent pTouchEvent) {

	}
}
